package com.yami.shop.personnft.api.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.*;
import com.yami.shop.config.ShopConfig;
import com.yami.shop.personnft.common.controller.BaseController;
import com.yami.shop.personnft.common.model.PersonNftAccount;
import com.yami.shop.personnft.common.service.PersonNftAccountService;
import com.yami.shop.personnft.common.service.PersonNftService;
import com.yami.shop.security.api.util.SecurityUtils;
import groovy.lang.Tuple;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * spu信息
 *
 * @author FrozenWatermelon
 * @date 2020-10-28 15:27:24
 */
@RestController("appPersonnftDtcAccountController")
@RequestMapping("/dtc/app/personnftAccount")
@Tag(name = "app-personnftAccount信息")
@AllArgsConstructor
public class PersonnftDtcAccountController extends BaseController {
    @Autowired
    private PersonNftAccountService personNftAccountService;
    @Autowired
    private ShopConfig shopConfig;
    @Autowired
    private DataWebService dataWebService;
    @Autowired
    private PersonNftService personNftService;
    /**
     *
     * @param page
     * @param personNftAccount
     * @return
     */
    @GetMapping("/selectListPage")
    @Operation(summary =  "获取平台信息列表", description = "分页获取平台信息列表")
    public ServerResponseEntity<IPage<JSONObject>> selectListPage(PageParam<PersonNftAccount> page, PersonNftAccount personNftAccount) throws Exception {
        String userId = SecurityUtils.getUser().getUserId();
//        if(StringUtils.isEmpty(RedisUtil.get("channelCode::"+userId))){
//            throw new YamiShopBindException("yami.chain.getcheck.fail");
//        }
//        Tuple<String> tuple = this.getChannelInfo(userId);
            personNftAccount.setUserId(userId);
//            personNftAccount.setChainCode(tuple.get(2));
//            personNftAccount.setChannelCode(tuple.get(0));
        IPage<PersonNftAccount> page1 = personNftAccountService.page(page, personNftAccount);
        IPage<JSONObject> res = new PageParam<>();
        res.setCurrent(page.getCurrent());
        List<PersonNftAccount> records = page1.getRecords();
        if(records.size()>0){
            for (PersonNftAccount account:records) {
                if(account.getProductId().equals(0l)){
                    Object o = RedisUtil.get("applyIssue::" + account.getDenomId());
                    if(ObjectUtil.isNull(o)){
                        personNftService.getApplyIssueNftInfoByDenomId(account.getDenomId());
                        o = RedisUtil.get("applyIssue::" + account.getDenomId());
                    }
                    if(!ObjectUtil.isNull(o)){
                        JSONObject obj = JSONObject.parseObject(o.toString());
                        account.setShortUrl(obj.getString("imgUrl"));
                        account.setDawName(obj.getString("dawName"));
                        account.setAuthor(obj.getString("author"));
                    }
                }
            }
        }
        List<JSONObject> personNfts = ObjectUtil.isNull(records)? Lists.newArrayList():JSONArray.parseArray(JSONObject.toJSONString(records), JSONObject.class);
        res.setRecords(personNfts);
        res.setSize(page.getSize());
        res.setTotal(page1.getTotal());
        return ServerResponseEntity.success(res);

    }
    @GetMapping("/getDetailById")
    @Operation(summary =  "查详情信息", description = "查详情信息")
    public ServerResponseEntity<JSONObject> getDetail(@RequestParam  String id) {
        String userId = SecurityUtils.getUser().getUserId();
        if(StringUtils.isEmpty(RedisUtil.get("channelCode::"+userId))){
            throw new YamiShopBindException("yami.chain.getcheck.fail");
        }
        PersonNftAccount personNftAccount = PersonNftAccount.builder().id(id).build();
        PersonNftAccount detail = personNftAccountService.getDetail(personNftAccount);
        String mall4JChannel = shopConfig.getMall4JChannel();
        JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(detail));
        if(StrUtil.isNotEmpty(mall4JChannel) && StringUtils.matches(detail.getChannelCode(), Arrays.asList(mall4JChannel.split(",")))){
            Object o = RedisUtil.get("applyIssue::" + detail.getDenomId());
            if(ObjectUtil.isNull(o)){
                personNftService.getApplyIssueNftInfoByDenomId(detail.getDenomId());
                o = RedisUtil.get("applyIssue::" + detail.getDenomId());
            }
            JSONObject obj = JSONObject.parseObject(o.toString());
            jsonObject.put("shortUrl", obj.getString("imgUrl"));
            jsonObject.put("power", 0);
        }else{
            jsonObject.put("power", detail.getCount());
        }
        return ServerResponseEntity.success(jsonObject);
    }
}
